<script>
	import triggerScale from '@/util/useScalePage.js'

	// let option = {
  //   targetX: 1366,
  //   targetY: 1024,
  //   targetRatio: 16 / 9
  // }
	export default {
		onLaunch: function() {
			// let dz =  uni.getStorageSync('dz') == '' ? this.upDateDz : uni.getStorageSync('dz')
			// http://hzserver.ywhzsoft.com:8888/update/ScPadUpdate/04712/update.apk
			// if (!uni.getStorageSync('bbh')) {
			// 	uni.setStorageSync('bbh', '1.0.0')
			// }
			let url = 'http://hzserver.ywhzsoft.com:8888/update/ScPadUpdate/04712/Update.txt' // 更新地址
			uni.request({
				// url: `http://${dz}/Update.txt`,
				url,
				method: 'GET',
				dataType: 'json',
				success: (r) => {
					let Version = uni.getStorageSync('bbh') == '' ? r.data.Version : uni.getStorageSync('bbh')
					if (!uni.getStorageSync('bbh')) {
						uni.setStorageSync('bbh', r.data.Version)
					}
					console.log(Version, r.data.Version, 'sss', !!r.data.Version && Version != r.data.Version)
					if (!!r.data.Version && Version != r.data.Version) {
						uni.showModal({
							content: '有新的版本发布，点击确定后不要退出,程序将启动自动更新。新版本下载完成后将自动弹出安装程序。',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									dtask.start();
									var prg = 0;
									var showLoading = plus.nativeUI.showWaiting("正在下载");
									dtask.addEventListener('statechanged', function(task,
										status) { //添加下载任务事件监听器
										// 给下载任务设置一个监听 并根据状态 做操作
										switch (task.state) {
											case 1:
												showLoading.setTitle("正在下载");
												break;
											case 2:
												showLoading.setTitle("已连接到服务器");
												break;
											case 3:
												prg = parseInt( //下载的进度
													(parseFloat(task
															.downloadedSize) /
														parseFloat(task.totalSize)
														) * 100
												);
												showLoading.setTitle("版本更新,正在下载" + prg + "% ");
												break;
											case 4:
									   plus.nativeUI.closeWaiting(); //关闭系统提示框
												//下载完成
												break;
										}
									});
								}
							}
						});
						var dtask = plus.downloader.createDownload(r.data.url, {
							force: true
						}, function(d,status) {
							// 下载完成    
							if (status == 200) {
								plus.runtime.install(plus.io.convertLocalFileSystemURL(d
									.filename), {},
									function(fileEntry) {
										//this.Version = r.data.Version
										uni.setStorageSync('bbh', r.data.Version)
									},
									function(error) {
										uni.showToast({
											title: '安装失败',
											mask: false,
											duration: 1500
										});
									})
							} else {
								uni.showToast({
									title: '更新失败',
									mask: false,
									duration: 1500
								});
							}
						});
					} else {
			
					}
			
			
				}
			})


			// //////////////////////////////////////////////
			
			// #ifdef APP-PLUS
			plus.screen.lockOrientation('landscape-primary'); // 强制横屏
			// #endif

			// uni.getSystemInfo({
			// 	success: function (info) {
			// 		let option = {
			// 			targetX: info.windowWidth,
			// 			targetY: info.windowHeight,
			// 			targetRatio: 16 / 9
			// 		}
			// 		triggerScale(option);  // 设备适配
			// 	}
			// });

		},
		onShow: function() {

		},
		onHide: function() {
		},
		onUnload: function() {

		},
		globalData: {
			getModel: function() {
				//获取手机型号
				return wx.getSystemInfoSync()["model"];
			},
			getVersion: function() {
				//获取微信版本号
				return wx.getSystemInfoSync()["version"];
			},
			getSystem: function() {
				//获取操作系统版本
				return wx.getSystemInfoSync()["system"];
			},
			getPlatform: function() {
				//获取客户端平台
				return wx.getSystemInfoSync()["platform"];
			},
			getSDKVersion: function() {
				//获取客户端基础库版本
				return wx.getSystemInfoSync()["SDKVersion"];
			},
			userInfo: null,
			platform: "",
			screenWidth: uni.getSystemInfoSync().screenWidth,
			screenHeight: uni.getSystemInfoSync().screenHeight,
			BLEInformation: { 
				platform: "",
				deviceId: "",
				writeCharaterId: "",
				writeServiceId: "",
				notifyCharaterId: "",
				notifyServiceId: "",
				readCharaterId: "",
				readServiceId: ""
			}
		},
	}
</script>

<style>
	/*每个页面公共css */
	@import "static/icon/iconfont.css";
	
	@media screen and(min-width:750px){
		html {
			font-size: 100%;
		}
	}
	/* 1rem = 37.5px */
	@media screen and(max-width:750px){
		html {
			font-size: 50%;
		}
	}
</style>
